package com.example.tally;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.tally.fragment.DetailFragment;
import com.example.tally.fragment.HomeFragment;
import com.example.tally.fragment.ListFragment;
import com.example.tally.fragment.SettingFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    //底部菜单栏3个TextView
    @BindView(R.id.tv_home)
    TextView mTabHome;
    @BindView(R.id.tv_list)
    TextView mTabList;
    @BindView(R.id.tv_detail)
    TextView mTabDetail;
    @BindView(R.id.tv_setting)
    TextView mTabSetting;

    //3个Fragment
    private Fragment mHomeFragment;
    private Fragment mListFragment;
    private Fragment mDetailFragment;
    private Fragment mSettingFragment;

    //标记当前显示的Fragment
    private int mFragmentId = 0;

    SharedPreferences preferences;

    // 标记三个Fragment
    public static final int FRAGMENT_Home = 0;
    public static final int FRAGMENT_List = 1;
    public static final int FRAGMENT_Detail = 2;
    public static final int FRAGMENT_Setting = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        preferences = getSharedPreferences("previousPos", Context.MODE_PRIVATE);


        ButterKnife.bind(this);
        //根据传入的Bundle对象判断Activity是正常启动还是销毁重建
        if (savedInstanceState == null) {
            //设置第一个Fragment默认选中
            setFragment(FRAGMENT_Home);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //通过onSaveInstanceState方法保存当前显示的fragment
        outState.putInt("fragment_id", mFragmentId);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        super.onRestoreInstanceState(savedInstanceState);
        FragmentManager mFragmentManager = getSupportFragmentManager();
        //通过FragmentManager获取保存在FragmentTransaction中的Fragment实例
        mHomeFragment = mFragmentManager.findFragmentByTag("home_fragment");
        mListFragment = mFragmentManager.findFragmentByTag("list_fragment");
        mDetailFragment = mFragmentManager.findFragmentByTag("detail_fragment");
        mSettingFragment = mFragmentManager.findFragmentByTag("setting_fragment");
        //恢复销毁前显示的Fragment
        setFragment(savedInstanceState.getInt("fragment_id"));
    }

    @OnClick({R.id.tv_home, R.id.tv_list, R.id.tv_detail, R.id.tv_setting})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_home:
                setFragment(FRAGMENT_Home);
                break;
            case R.id.tv_list:
                setFragment(FRAGMENT_List);
                break;
            case R.id.tv_detail:
                setFragment(FRAGMENT_Detail);
                break;
            case R.id.tv_setting:
                setFragment(FRAGMENT_Setting);
                break;
        }
    }

    private void setFragment(int index) {
        //获取Fragment管理器
        FragmentManager mFragmentManager = getSupportFragmentManager();
        //开启事务
        FragmentTransaction mTransaction = mFragmentManager.beginTransaction();
        //隐藏所有Fragment
        hideFragments(mTransaction);
        switch (index) {
            default:
                break;
            case FRAGMENT_Home:
                mFragmentId = FRAGMENT_Home;
                //设置菜单栏为选中状态（修改文字和图片颜色）
                mTabHome.setTextColor(getResources().getColor(R.color.dark_green));
                mTabHome.setCompoundDrawablesWithIntrinsicBounds(0,
                        R.mipmap.home_green, 0, 0);
                //显示对应Fragment
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    mTransaction.add(R.id.container, mHomeFragment, "home_fragment");
                } else {
                    mTransaction.show(mHomeFragment);
                }
                break;
            case FRAGMENT_List:
                mFragmentId = FRAGMENT_List;
                mTabList.setTextColor(getResources().getColor(R.color.dark_green));
                mTabList.setCompoundDrawablesWithIntrinsicBounds(0,
                        R.mipmap.list_green, 0, 0);
                if (mListFragment == null) {
                    mListFragment = new ListFragment();
                    mTransaction.add(R.id.container, mListFragment, "list_fragment");
                } else {
                    mTransaction.show(mListFragment);
                }
                break;
            case FRAGMENT_Detail:
                mFragmentId = FRAGMENT_Detail;
                mTabDetail.setTextColor(getResources().getColor(R.color.dark_green));
                mTabDetail.setCompoundDrawablesWithIntrinsicBounds(0,
                        R.mipmap.detail_green, 0, 0);
                if (mDetailFragment == null) {
                    mDetailFragment = new DetailFragment();
                    mTransaction.add(R.id.container, mDetailFragment, "detail_fragment");
                } else {
                    mTransaction.show(mDetailFragment);
                }
                break;
            case FRAGMENT_Setting:
                mFragmentId = FRAGMENT_Setting;
                mTabSetting.setTextColor(getResources().getColor(R.color.dark_green));
                mTabSetting.setCompoundDrawablesWithIntrinsicBounds(0,
                        R.mipmap.setting_green, 0, 0);
                if (mSettingFragment == null) {
                    mSettingFragment = new SettingFragment();
                    mTransaction.add(R.id.container, mSettingFragment, "setting_fragment");
                } else {
                    mTransaction.show(mSettingFragment);
                }
                break;
        }
        //提交事务
        mTransaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mHomeFragment != null) {
            //隐藏Fragment
            transaction.hide(mHomeFragment);
            //将对应菜单栏设置为默认状态
            mTabHome.setTextColor(getResources().getColor(R.color.black_record));
            mTabHome.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.mipmap.home, 0, 0);
        }
        if (mListFragment != null) {
            transaction.hide(mListFragment);
            mTabList.setTextColor(getResources().getColor(R.color.black_record));
            mTabList.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.mipmap.list, 0, 0);
        }
        if (mDetailFragment != null) {
            transaction.hide(mDetailFragment);
            mTabDetail.setTextColor(getResources().getColor(R.color.black_record));
            mTabDetail.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.mipmap.detail, 0, 0);
        }
        if (mSettingFragment != null) {
            transaction.hide(mSettingFragment);
            mTabSetting.setTextColor(getResources().getColor(R.color.black_record));
            mTabSetting.setCompoundDrawablesWithIntrinsicBounds(0,
                    R.mipmap.detail, 0, 0);
        }
    }
}
